package com.redstars.generate.controller;

import com.redstars.generate.result.Result;
import com.redstars.generate.service.SysGeneratorService;
import com.redstars.generate.vo.SysGenerateTemplateVo;
import com.redstars.generate.vo.SysPreviewFileVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author : zhouhx
 * @date : 2022/10/18 15:19
 */
@Controller
@RequestMapping("gen/generator")
@AllArgsConstructor
public class SysGeneratorController {
    private final SysGeneratorService generatorService;

    /**
     * 生成代码（zip压缩包）
     */
    @GetMapping("download")
    public void download(String tableIds, HttpServletResponse response) throws Exception {
        generatorService.download(tableIds,response);
    }

    /**
     * 生成代码（自定义目录）
     */
    @ResponseBody
    @PostMapping("code")
    public Result<String> code(@RequestBody Long[] tableIds) throws Exception {
        generatorService.generatorCode(tableIds);
        return Result.ok();
    }

    /**
     * 预览代码
     */
    @ResponseBody
    @PostMapping("preview")
    public Result<List<SysPreviewFileVo>> preview(@RequestBody Long tableId) throws Exception {
        List<SysPreviewFileVo> preview = generatorService.preview(tableId);
        return Result.ok(preview);
    }
}
